#!/usr/bin/env node

const http = require('http'),
      log  = console.log;
      baseURL = 'http://localhost:8080';

function get(addr) {
  return new Promise(resolve => {
    http.get(addr, res => {
      log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
      log(res.headers);
      log();
  
      res.pipe(process.stdout);
      let url = res.statusCode >= 300 && res.statusCode < 400 ?
      res.headers.location : '';
      resolve(url);
    });
  });
}


async function main() {
  let url = '/';
  do {
    url = await get(baseURL+url);  //url = '/abc' ,= '/def' ,= '',
  } while(url !== '');
}

main();
